package com.liam.chinesevietnamesecorpus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liam.chinesevietnamesecorpus.common.BaseController;
import com.liam.chinesevietnamesecorpus.common.R;
import com.liam.chinesevietnamesecorpus.dto.BatchUploadResultDTO;
import com.liam.chinesevietnamesecorpus.dto.CorpusItemDTO;
import com.liam.chinesevietnamesecorpus.dto.CorpusItemUpdateDTO;
import com.liam.chinesevietnamesecorpus.service.CorpusItemService;
import com.liam.chinesevietnamesecorpus.vo.CorpusItemVO;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 语料控制器
 * 
 * @author Liam
 */
@Slf4j
@RestController
@RequestMapping("/items")
public class CorpusItemController extends BaseController {

    @Autowired
    private CorpusItemService corpusItemService;

    /**
     * 创建语料
     */
    @PostMapping("/create")
    public R<CorpusItemVO> createCorpusItem(@Valid @RequestBody CorpusItemDTO corpusItemDTO) {
        log.info("创建语料请求：语料库ID={}", corpusItemDTO.getLibraryId());
        CorpusItemVO corpusItemVO = corpusItemService.createCorpusItem(corpusItemDTO);
        log.info("语料创建成功：语料库ID={}", corpusItemDTO.getLibraryId());
        return R.ok("创建成功", corpusItemVO);
    }

    /**
     * 获取语料信息
     */
    @GetMapping("/get/{id}")
    public R<CorpusItemVO> getCorpusItem(@PathVariable Long id) {
        CorpusItemVO corpusItemVO = corpusItemService.getCorpusItemById(id);
        return R.ok(corpusItemVO);
    }

    /**
     * 更新语料信息
     */
    @PutMapping("/update/{id}")
    public R<CorpusItemVO> updateCorpusItem(@PathVariable Long id, @Valid @RequestBody CorpusItemUpdateDTO corpusItemUpdateDTO) {
        log.info("更新语料请求：语料ID={}", id);
        CorpusItemVO corpusItemVO = corpusItemService.updateCorpusItem(id, corpusItemUpdateDTO);
        log.info("语料信息更新成功：语料ID={}", id);
        return R.ok("更新成功", corpusItemVO);
    }

    /**
     * 删除语料
     */
    @DeleteMapping("/delete/{id}")
    public R<Void> deleteCorpusItem(@PathVariable Long id) {
        log.info("删除语料请求：语料ID={}", id);
        corpusItemService.deleteCorpusItem(id);
        log.info("语料删除成功：语料ID={}", id);
        return R.ok("删除成功");
    }

    /**
     * 获取语料列表（分页）
     */
    @GetMapping("/list")
    public R<IPage<CorpusItemVO>> getCorpusItemList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) Long libraryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String sourceText,
            @RequestParam(required = false) String targetText) {
        
        log.info("获取语料列表请求：page={}, size={}, libraryId={}, keyword={}, sourceText={}, targetText={}", 
                page, size, libraryId, keyword, sourceText, targetText);
        IPage<CorpusItemVO> itemPage = corpusItemService.getCorpusItemList(page, size, libraryId, keyword, sourceText, targetText);
        return R.ok(itemPage);
    }

    /**
     * 批量上传语料
     */
    @PostMapping("/batch-upload")
    public R<BatchUploadResultDTO> batchUploadCorpusItems(
            @RequestParam("file") MultipartFile file,
            @RequestParam("libraryId") Long libraryId,
            @RequestParam("hasIndexColumn") Boolean hasIndexColumn) {
        
        log.info("批量上传语料请求：libraryId={}, hasIndexColumn={}, fileName={}", 
                libraryId, hasIndexColumn, file.getOriginalFilename());
        
        try {
            BatchUploadResultDTO result = corpusItemService.batchUploadCorpusItems(file, libraryId, hasIndexColumn);
            log.info("批量上传完成：成功{}条，失败{}条", result.getSuccessCount(), result.getFailCount());
            return R.ok("批量上传完成", result);
        } catch (Exception e) {
            log.error("批量上传失败：{}", e.getMessage(), e);
            return R.fail("批量上传失败：" + e.getMessage());
        }
    }
}
